<template>
  <el-menu
    :default-active="activeMenu"
    class="sidebar-menu"
    :collapse="isCollapse"
    :unique-opened="true"
    ref="menu"
    background-color="#304156"
    text-color="#bfcbd9"
    active-text-color="#409EFF"
    @select="handleSelect"
    @open="handleOpen">

    <template v-for="menu in routes">
      <!-- 没有子菜单的情况 -->
      <el-menu-item
          v-if="!menu.children"
          :key="menu.path"
          :index="'/main/' + menu.path">
        <i :class="menu.icon"></i>
        <span slot="title">{{ menu.menuName }}</span>
      </el-menu-item>

      <!-- 有子菜单的情况 -->
      <el-submenu
        v-else
        :key="menu.path"
        :index="'/main/' + menu.path">
        <template slot="title">
          <i :class="menu.icon"></i>
          <span>{{ menu.menuName }}</span>
        </template>
        
        <el-menu-item
            v-for="child in menu.children"
            :key="child.path"
            :index="'/main/' + menu.path + '/' + child.path">
          <i :class="child.icon"></i>
          <span>{{ child.menuName }}</span>
        </el-menu-item>
      </el-submenu>
    </template>
  </el-menu>
</template>

<script>
export default {
  name: 'SidebarMenu',
  props: {
    isCollapse: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      openedSubmenu: ''
    }
  },
  computed: {
    routes() {
      // 过滤掉按钮级别的菜单（type === 3）
      const filterMenus = (menus) => {
        return menus.filter(menu => {
          if (menu.type === 3) return false
          if (menu.children) {
            menu.children = filterMenus(menu.children)
          }
          return true
        })
      }

      return filterMenus(this.$store.state.menuTree || [])
    },
    activeMenu() {
      return this.$route.path
    }
  },
  methods: {
    handleSelect(index) {
      if (this.$route.path !== index) {
        this.$router.push(index)
      }
      const openedMenus = this.$refs.menu.openedMenus
      openedMenus.forEach(item => {
        this.$refs.menu.closeMenu(item)
      })
      const parentPath = index.split('/').slice(0, -1).join('/')
      if (parentPath !== '/main') {
        this.$refs.menu.openMenu(parentPath)
      }
    },
    handleOpen(index) {
      if (this.openedSubmenu && this.openedSubmenu !== index) {
        this.$refs.menu.closeMenu(this.openedSubmenu)
      }
      this.openedSubmenu = index
    }
  },
  watch: {
    '$route': {
      handler(to) {
        const openedMenus = this.$refs.menu?.openedMenus || []
        openedMenus.forEach(item => {
          this.$refs.menu.closeMenu(item)
        })
        const parentPath = to.path.split('/').slice(0, -1).join('/')
        if (parentPath !== '/main' && this.$refs.menu) {
          this.$refs.menu.openMenu(parentPath)
        }
      },
      immediate: true
    }
  }
}
</script>

<style lang="scss" scoped>
.sidebar-menu {
  height: calc(100% - 60px);
  border-right: none;
  overflow: hidden;

  &:not(.el-menu--collapse) {
    width: auto;
    min-width: 200px;
    max-width: 300px;
  }

  &.el-menu--collapse {
    width: 64px;
    .el-submenu__title span {
      display: none;
    }
    .el-submenu__title .el-submenu__icon-arrow {
      display: none;
    }
  }

  .el-submenu .el-menu-item {
    min-width: unset;
    padding: 0 20px 0 54px !important;

    &.is-active {
      background-color: #263445 !important;
    }
  }

  .el-menu-item, .el-submenu__title {
    height: 56px;
    line-height: 56px;
    padding: 0 20px !important;
    
    &:hover {
      background-color: #263445 !important;
    }
  }

  .el-menu-item [class^="el-icon-"],
  .el-submenu [class^="el-icon-"] {
    margin-right: 12px;
    width: 24px;
    text-align: center;
    font-size: 18px;
    vertical-align: middle;
  }
}

.el-menu-item, .el-submenu__title {
  i {
    vertical-align: middle;
    margin-right: 5px;
    width: 24px;
    text-align: center;
  }
  
  span {
    vertical-align: middle;
    margin-left: 5px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
</style> 